package com.future.mayi.qsale.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.mayi.activity.GoodsActivity;
import com.future.mayi.adapter.BaseRecyclerViewAdapter;
import com.future.mayi.qsale.model.CartListModel;
import com.future.mayi.utils.PriceUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/14.
 */

public class CartGoodsAdapter extends BaseRecyclerViewAdapter<CartListModel> {

    /**
     * 是否是结算模式
     */
    private boolean isCountType = true;

    public CartGoodsAdapter(Context context, List<CartListModel> list) {
        super(context, list);
    }

    public void setIsCountType(boolean isCountType) {
        this.isCountType = isCountType;
        notifyDataSetChanged();
    }

    public boolean isCountType() {
        return isCountType;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CartGoodsViewHolder(getView(parent, R.layout.item_cart_goods));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CartListModel cartGoodsModel = mList.get(position);
        if (cartGoodsModel == null) return;
        bindData((CartGoodsViewHolder) holder, cartGoodsModel, position);

    }

    private void bindData(CartGoodsViewHolder viewHolder, CartListModel cartGoodsModel, final int position) {

        if (isCountType) {
            viewHolder.cbCart.setChecked(cartGoodsModel.isSettleSelect());
        } else {
            viewHolder.cbCart.setChecked(cartGoodsModel.isDeleteSelect());
        }

        viewHolder.cbCart.setVisibility((isCountType && cartGoodsModel.getStatus() > 0) ? View.INVISIBLE : View.VISIBLE);
        viewHolder.cbCart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isCountType) {
                    if (mList.get(position).getStatus() > 0 && isChecked) {
                        buttonView.setChecked(false);
                        return;
                    }
                    mList.get(position).setSettleSelect(isChecked);
                    if (CartGoodsAdapter.this.mOnCountChangeListener != null) {
                        mOnCountChangeListener.onCountChange(position, isChecked);
                    }
                } else {
                    mList.get(position).setDeleteSelect(isChecked);
                }


                if (mOnCountChangeListener != null) {
                    mOnCountChangeListener.onCheckedChange();
                }
            }
        });

        viewHolder.tvTitle.setText(cartGoodsModel.getGoods_name());
        loadImg(viewHolder.ivImg, cartGoodsModel.getImages());
        viewHolder.tvPrice.setText(PriceUtils.getPriceText2(cartGoodsModel.getGoods_price()));
        if (cartGoodsModel.getRebate_price() > 0 || cartGoodsModel.getMin_rebate_price() > 0 || cartGoodsModel.getMax_rebate_price() > 0) {
            viewHolder.llZhuan.setVisibility(View.VISIBLE);
            String rabate = "";
            if (cartGoodsModel.getMin_rebate_price() > 0 || cartGoodsModel.getMax_rebate_price() > 0) {
                if (cartGoodsModel.getMax_rebate_price() == cartGoodsModel.getMin_rebate_price()) {
                    rabate = PriceUtils.getPriceText2(cartGoodsModel.getMin_rebate_price());
                } else {
                    rabate = PriceUtils.getPriceText2(cartGoodsModel.getMin_rebate_price()) + "-" + PriceUtils.getPriceText2(cartGoodsModel.getMax_rebate_price());
                }
            } else {
                rabate = rabate + PriceUtils.getPriceText2(cartGoodsModel.getRebate_price());
            }
            setText(viewHolder.tvZhuanPrice, rabate);
        } else {
            viewHolder.llZhuan.setVisibility(View.GONE);
        }
        viewHolder.tvNull.setVisibility(cartGoodsModel.getStatus() > 0 ? View.VISIBLE : View.GONE);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CartListModel model = mList.get(position);
                if (model == null) return;
                if (model.getStatus() > 0) return;
                GoodsActivity.startActivity(mContext, model.getGoods_id());
            }
        });

        setText(viewHolder.tvValue, cartGoodsModel.getSpec_value() + "，x" + cartGoodsModel.getBuy_num());
//        setText(viewHolder.tvGoodsNum, String.valueOf(cartGoodsModel.getBuy_num()));


    }

    private OnCountChangeListener mOnCountChangeListener;

    public void setOnCountChangeListener(OnCountChangeListener onCountChangeListener) {
        this.mOnCountChangeListener = onCountChangeListener;
    }

    public interface OnCountChangeListener {
        void onCountChange(int position, boolean isChecked);

        /**
         * 选中状态改变,刷新按钮颜色
         */
        void onCheckedChange();
    }

    public void selectAll(boolean selectAll) {
        if (mList == null) return;
        if (mList.size() == 0) return;
        for (int i = 0; i < mList.size(); i++) {
            if (isCountType) {
                if (mList.get(i).getStatus() > 0) continue;
                mList.get(i).setSettleSelect(selectAll);
            } else {
                mList.get(i).setDeleteSelect(selectAll);
            }
        }
        notifyDataSetChanged();
    }

    public static class CartGoodsViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.cb_cart)
        CheckBox cbCart;
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_dolor)
        TextView tvDolor;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_value)
        TextView tvValue;
        //        @BindView(R.id.tv_goods_num)
//        TextView tvGoodsNum;
        @BindView(R.id.tv_null)
        TextView tvNull;
        @BindView(R.id.ll_zhuan)
        View llZhuan;
        @BindView(R.id.tv_zhuan_price)
        TextView tvZhuanPrice;

        public CartGoodsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
